<?php
/**
* @file $Id: Address.inc.php 543 2007-06-03 22:02:50Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if($_REQUEST['values'] && $_POST['values'])
{
	if($_REQUEST['values']['ADDRESS'])
	{
		if($_REQUEST['address_id']!='new')
		{
			$sql = "UPDATE ADDRESS SET ";
							
			foreach($_REQUEST['values']['ADDRESS'] as $column=>$value)
			{
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			}
			$sql = substr($sql,0,-1) . " WHERE ADDRESS_ID='$_REQUEST[address_id]'";
			DBQuery($sql);
		}
		else
		{
			$id = DBGet(DBQuery('SELECT '.db_seq_nextval('ADDRESS_SEQ').' as SEQ_ID '.FROM_DUAL));
			$id = $id[1]['SEQ_ID'];

			$sql = "INSERT INTO ADDRESS ";

			$fields = 'ADDRESS_ID,';
			$values = "'".$id."',";

			$go = 0;
			foreach($_REQUEST['values']['ADDRESS'] as $column=>$value)
			{
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";

					if($column=='PHONE')
						$column = 'AREACODE';
					if(SystemPreferences('DEFAULT_'.$column)!=$value)
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
			if($go)
			{
				DBQuery($sql);
				DBQuery("INSERT INTO STUDENTS_JOIN_ADDRESS (ID,STUDENT_ID,ADDRESS_ID,RESIDENCE,MAILING,BUS_PICKUP,BUS_DROPOFF) values(".db_seq_nextval('STUDENTS_JOIN_ADDRESS_SEQ').",'".UserStudentID()."','".$id."','".$_REQUEST['values']['STUDENTS_JOIN_ADDRESS']['RESIDENCE']."','".$_REQUEST['values']['STUDENTS_JOIN_ADDRESS']['MAILING']."','".$_REQUEST['values']['STUDENTS_JOIN_ADDRESS']['BUS_PICKUP']."','".$_REQUEST['values']['STUDENTS_JOIN_ADDRESS']['BUS_DROPOFF']."')");
				$_REQUEST['address_id'] = $id;
				$_REQUEST['person_id']='new';
			}
		}
	}
	
	if($_REQUEST['values']['PEOPLE'])
	{
		foreach($_REQUEST['values']['PEOPLE'] as $person_id=>$person)
		{
			if($person_id!='new' && $person_id!='new2' && $person_id!='newe')
			{
				$sql = "UPDATE PEOPLE SET ";
								
				foreach($person as $column=>$value)
				{
					$sql .= $column."='".str_replace("\'","''",$value)."',";
				}
				$sql = substr($sql,0,-1) . " WHERE PERSON_ID='$person_id'";
				DBQuery($sql);
			}
			elseif($_REQUEST['address_id']!='new' || $person_id=='newe')
			{
				$id = DBGet(DBQuery('SELECT '.db_seq_nextval('PEOPLE_SEQ').' as SEQ_ID '.FROM_DUAL));
				$id = $id[1]['SEQ_ID'];
	
				$sql = "INSERT INTO PEOPLE ";
	
				$fields = 'PERSON_ID,';
				$values = "'".$id."',";
	
				$go = 0;
				foreach($person as $column=>$value)
				{
					if($value)
					{
						$fields .= $column.',';
						$values .= "'".str_replace("\'","''",$value)."',";
						$go = true;
					}
				}
				$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
				if($go)
				{
					if($person_id=='newe')
						$person_address_id = 0;
					else
						$person_address_id = $_REQUEST['address_id'];

					DBQuery($sql);
					DBQuery("INSERT INTO STUDENTS_JOIN_PEOPLE (ID,PERSON_ID,STUDENT_ID,ADDRESS_ID,CUSTODY,EMERGENCY,STUDENT_RELATION) values(".db_seq_nextval('STUDENTS_JOIN_PEOPLE_SEQ').",'$id','".UserStudentID()."','".$person_address_id."','".$_REQUEST['values']['STUDENTS_JOIN_PEOPLE'][$person_id]['CUSTODY']."','".$_REQUEST['values']['STUDENTS_JOIN_PEOPLE'][$person_id]['EMERGENCY']."','".$_REQUEST['values']['STUDENTS_JOIN_PEOPLE'][$person_id]['STUDENT_RELATION']."')");
					if(!is_numeric($_REQUEST['person_id']))
						$_REQUEST['person_id'] = $id;
				}
			}
			else
				BackPrompt(_('You must enter an address.'));
		}
	}
	
	if($_REQUEST['values']['PEOPLE_JOIN_CONTACTS'])
	{
		foreach($_REQUEST['values']['PEOPLE_JOIN_CONTACTS'] as $id=>$values)
		{
			if(substr($id,0,3)!='new')
			{
				$sql = "UPDATE PEOPLE_JOIN_CONTACTS SET ";
								
				foreach($values as $column=>$value)
				{
					$sql .= $column."='".str_replace("\'","''",$value)."',";
				}
				$sql = substr($sql,0,-1) . " WHERE ID='$id'";
				DBQuery($sql);
			}
			else
			{
				if($values['TITLE'] && $values['TITLE']!='Example Phone' && $values['VALUE'] && $values['VALUE']!='(xxx) xxx-xxxx')
				{
					$sql = "INSERT INTO PEOPLE_JOIN_CONTACTS ";
		
					$fields = 'ID,PERSON_ID,';
					$vals = db_seq_nextval('PEOPLE_JOIN_CONTACTS_SEQ').",'".$_REQUEST['person_id']."',";
		
					$go = 0;
					foreach($values as $column=>$value)
					{
						if($value)
						{
							$fields .= $column.',';
							$vals .= "'".str_replace("\'","''",$value)."',";
							$go = true;
						}
					}
					$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($vals,0,-1) . ')';
					if($go)
						DBQuery($sql);				
				}
			}
		}
	}

	if($_REQUEST['values']['STUDENTS_JOIN_PEOPLE'])
	{
		$sql = "UPDATE STUDENTS_JOIN_PEOPLE SET ";

		foreach($_REQUEST['values']['STUDENTS_JOIN_PEOPLE'] as $person_id=>$sjp)
		{
			if($person_id!='new' && $person_id!='new2' && $person_id!='newe')
			{
				foreach($sjp as $column=>$value)
				{
					$sql .= $column."='".str_replace("\'","''",$value)."',";
				}
				$sql = substr($sql,0,-1) . " WHERE PERSON_ID='$person_id' AND STUDENT_ID='".UserStudentID()."'";
				DBQuery($sql);
			}
		}
	}

	if($_REQUEST['values']['STUDENTS_JOIN_ADDRESS'] && $_REQUEST['address_id']!='new')
	{
		$sql = "UPDATE STUDENTS_JOIN_ADDRESS SET ";
						
		foreach($_REQUEST['values']['STUDENTS_JOIN_ADDRESS'] as $column=>$value)
		{
			$sql .= $column."='".str_replace("\'","''",$value)."',";
		}
		$sql = substr($sql,0,-1) . " WHERE ADDRESS_ID='$_REQUEST[address_id]' AND STUDENT_ID='".UserStudentID()."'";
		DBQuery($sql);
	}
	unset($_REQUEST['modfunc']);
	unset($_REQUEST['values']);
	if($_REQUEST['person_id']=='new')
		unset($_REQUEST['person_id']);
	unset($_SESSION['_REQUEST_vars']['values']);
}
unset($_SESSION['_REQUEST_vars']['address_id']);
unset($_SESSION['_REQUEST_vars']['person_id']);

if($_REQUEST['modfunc']=='delete')
{
	if($_REQUEST['sibling_student_id'])
	{
		$student_RET = DBGet(DBQuery("SELECT FIRST_NAME,LAST_NAME FROM STUDENTS WHERE STUDENT_ID='".$_SESSION['student_id']."'"));
		$sibling_RET = DBGet(DBQuery("SELECT FIRST_NAME,LAST_NAME FROM STUDENTS WHERE STUDENT_ID='".$_REQUEST['sibling_student_id']."'"));
		if(Prompt(_('Confirm'),_('Are you sure that you would like to copy the addresses and contacts from ').$sibling_RET[1]['FIRST_NAME'].' '.$sibling_RET[1]['LAST_NAME']._(' to ').$student_RET[1]['FIRST_NAME'].' '.$student_RET[1]['LAST_NAME'].'?'))
		{
			$addresses_RET = DBGet(DBQuery("SELECT ADDRESS_ID,MAILING,RESIDENCE,BUS_PICKUP,BUS_DROPOFF FROM STUDENTS_JOIN_ADDRESS WHERE STUDENT_ID='".$_REQUEST['sibling_student_id']."'"));
			foreach($addresses_RET as $address)
			{
				DBQuery("INSERT INTO STUDENTS_JOIN_ADDRESS (ID,STUDENT_ID,ADDRESS_ID,MAILING,RESIDENCE,BUS_PICKUP,BUS_DROPOFF) values(".db_seq_nextval('STUDENTS_JOIN_ADDRESS_SEQ').",'".$_SESSION['student_id']."','$address[ADDRESS_ID]','$address[MAILING]','$address[RESIDENCE]','$address[BUS_PICKUP]','$address[BUS_DROPOFF]')");
				$_REQUEST['address_id'] = $address['ADDRESS_ID'];
			}
			$person_RET = DBGet(DBQuery("SELECT PERSON_ID,ADDRESS_ID,CUSTODY,EMERGENCY,STUDENT_RELATION FROM STUDENTS_JOIN_PEOPLE WHERE STUDENT_ID='".$_REQUEST['sibling_student_id']."'"));
			foreach($person_RET as $person)
			{
				DBQuery("INSERT INTO STUDENTS_JOIN_PEOPLE (ID,STUDENT_ID,PERSON_ID,ADDRESS_ID,CUSTODY,EMERGENCY,STUDENT_RELATION) values(".db_seq_nextval('STUDENTS_JOIN_PEOPLE_SEQ').",'".$_SESSION['student_id']."','$person[PERSON_ID]','$person[ADDRESS_ID]','$person[CUSTODY]','$person[EMERGENCY]','$person[STUDENT_RELATION]')");
			}
			unset($_REQUEST['modfunc']);
			unset($_REQUEST['sibling_student_id']);
		}
	}
	elseif($_REQUEST['contact_id'])
	{
		if(DeletePrompt(_('contact information')))
		{
			DBQuery("DELETE FROM PEOPLE_JOIN_CONTACTS WHERE ID='$_REQUEST[contact_id]'");
			unset($_REQUEST['modfunc']);
		}	
	}
	elseif($_REQUEST['person_id'])
	{
		if(DeletePrompt(_('contact')))
		{
			DBQuery("DELETE FROM STUDENTS_JOIN_PEOPLE WHERE PERSON_ID='$_REQUEST[person_id]' AND STUDENT_ID='".UserStudentID()."'");
			$other_RET = DBGet(DBQuery("SELECT STUDENT_ID FROM STUDENTS_JOIN_PEOPLE WHERE PERSON_ID='".$_REQUEST['person_id']."' AND STUDENT_ID!='".$_SESSION['student_id']."'"));
			if(!count($other_RET))
			{
				DBQuery("DELETE FROM PEOPLE WHERE PERSON_ID='$_REQUEST[person_id]'");
				DBQuery("DELETE FROM PEOPLE_JOIN_CONTACTS WHERE PERSON_ID='$_REQUEST[person_id]'"); // add this one 
			}
			unset($_REQUEST['modfunc']);
			unset($_REQUEST['person_id']);
		}	
	}
	elseif($_REQUEST['address_id'])
	{
		if(DeletePrompt(_('address')))
		{
			DBQuery("UPDATE STUDENTS_JOIN_PEOPLE SET ADDRESS_ID='0' WHERE ADDRESS_ID='$_REQUEST[address_id]'");
			DBQuery("DELETE FROM STUDENTS_JOIN_ADDRESS WHERE ADDRESS_ID='$_REQUEST[address_id]' AND STUDENT_ID='$_SESSION[student_id]'");
			$other_RET = DBGet(DBQuery("SELECT STUDENT_ID FROM STUDENTS_JOIN_ADDRESS WHERE ADDRESS_ID='".$_REQUEST['address_id']."' AND STUDENT_ID!='".$_SESSION['student_id']."'"));
			if(!count($other_RET))
				DBQuery("DELETE FROM ADDRESS WHERE ADDRESS_ID='$_REQUEST[address_id]'");
			unset($_REQUEST['modfunc']);
			unset($_REQUEST['address_id']);
		}
	}
}
if($_FOCUS['direction']=='RTL')
	$arrow_right = 'arrow_left';
else
	$arrow_right = 'arrow_right';


if(!$_REQUEST['modfunc'])
{
	$addresses_RET = DBGet(DBQuery("SELECT a.ADDRESS_ID,sjp.STUDENT_RELATION,a.ADDRESS,a.CITY,a.STATE,a.ZIPCODE,a.PHONE,a.MAIL_ADDRESS,a.MAIL_CITY,a.MAIL_STATE,A.MAIL_ZIPCODE,sjp.CUSTODY,sja.MAILING,sja.RESIDENCE,sja.BUS_PICKUP,sja.BUS_DROPOFF FROM ADDRESS a,STUDENTS_JOIN_ADDRESS sja,STUDENTS_JOIN_PEOPLE sjp WHERE a.ADDRESS_ID=sja.ADDRESS_ID AND a.ADDRESS_ID=sjp.ADDRESS_ID AND sjp.STUDENT_ID=sja.STUDENT_ID AND sja.STUDENT_ID='".UserStudentID()."' 
							  UNION SELECT a.ADDRESS_ID,'No Contacts' AS STUDENT_RELATION,a.ADDRESS,a.CITY,a.STATE,a.ZIPCODE,a.PHONE,a.MAIL_ADDRESS,a.MAIL_CITY,a.MAIL_STATE,A.MAIL_ZIPCODE,'' AS CUSTODY,sja.MAILING,sja.RESIDENCE,sja.BUS_PICKUP,sja.BUS_DROPOFF FROM ADDRESS a,STUDENTS_JOIN_ADDRESS sja WHERE a.ADDRESS_ID=sja.ADDRESS_ID AND sja.STUDENT_ID='".UserStudentID()."' AND NOT EXISTS (SELECT '' FROM STUDENTS_JOIN_PEOPLE sjp WHERE sjp.STUDENT_ID=sja.STUDENT_ID AND sjp.ADDRESS_ID=a.ADDRESS_ID) ORDER BY CUSTODY ASC,STUDENT_RELATION"),array(),array('ADDRESS_ID','STUDENT_RELATION'));
	$no_address_RET = DBGet(DBQuery("SELECT STUDENT_RELATION FROM STUDENTS_JOIN_PEOPLE WHERE STUDENT_ID='".UserStudentID()."' AND ADDRESS_ID='0'"));
	if(count($no_address_RET))
	{
		foreach($no_address_RET as $relation)
			$relation_list .= $relation['STUDENT_RELATION'].', ';
		$relation_list = substr($relation_list,0,-2);
		$addresses_RET['0'][$relation_list][1] = array('ADDRESS_ID'=>'0','STUDENT_RELATION'=>$relation_list,'ADDRESS'=>'No Address');
	}
	if(count($addresses_RET)==1 && $_REQUEST['address_id']!='new' && $_REQUEST['address_id']!='0' && $_REQUEST['address_id']!='existing')
		$_REQUEST['address_id'] = key($addresses_RET);
	
	echo '<TABLE><TR><TD valign=top>';
	echo '<TABLE border=0 cellpadding=0 cellspacing=0>';
	if(count($addresses_RET)>1 || $_REQUEST['address_id']=='new' || $_REQUEST['address_id']=='0' || $_REQUEST['address_id']=='existing')
	{
		$i = 1;
		if(!isset($_REQUEST['address_id']))
			$_REQUEST['address_id'] = key($addresses_RET);

		if(count($addresses_RET))
		{
			foreach($addresses_RET as $address_id=>$relations)
			{
				$relation_list = '';
				echo '<TR><TD colspan=3 style="border:1; border-style: none none solid none;">';
	
				foreach($relations as $relation=>$addresses)
					$relation_list .= $relation.', ';
				$address = $addresses[1];
				$relation_list = substr($relation_list,0,-2);
	
				echo '<B>'.$relation_list.'</B> ';
				if($address['RESIDENCE']=='Y')
					echo '<IMG SRC=assets/house_button.gif> ';
				if($address['MAILING']=='Y')
					echo '<IMG SRC=assets/mailbox_button.gif> ';
				if($address['BUS_PICKUP']=='Y' || $address['BUS_DROPOFF']=='Y')
					echo '<IMG SRC=assets/bus_button.gif> ';
				echo '</TD></TR>';
			
				//$address = $relations[key($relations)][1];
				if($i!=count($addresses_RET))
					$style = ' style="border:1; border-style: none none dashed none;"';
				elseif($i!=1)
					$style = ' style="border:1; border-style: dashed none none none;"';
				$style .= ' ';
	
				if($address_id==$_REQUEST['address_id'] && $_REQUEST['address_id']!=='0' && $_REQUEST['address_id']!='new')
					$this_address = $address;
				
				$i++;
				//echo '<A style="cursor:hand;">';
				$link = 'onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id='.$address['ADDRESS_ID']."&LO_index=".$_REQUEST['LO_index'].'\';"';
				if($_REQUEST['address_id']==$address['ADDRESS_ID'])
					echo '<TR bgcolor=#'.Preferences('HIGHLIGHT').'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.(($address['ADDRESS_ID']!='0' && AllowEdit())?button('remove','',"Modules.php?modname=$_REQUEST[modname]&include=$_REQUEST[include]&address_id=$address[ADDRESS_ID]&modfunc=delete&LO_index=".$_REQUEST['LO_index'],20):'').'</TD><TD '.$link.' style="color:white;" '.$style.'>';
				else
					echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id='.$address['ADDRESS_ID']."&LO_index=".$_REQUEST['LO_index'].'\';" onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.(($address['ADDRESS_ID']!='0' && AllowEdit())?button('remove','',"Modules.php?modname=$_REQUEST[modname]&include=$_REQUEST[include]&address_id=$address[ADDRESS_ID]&modfunc=delete&LO_index=".$_REQUEST['LO_index'],20):'').'</TD><TD '.$link.' '.$style.'>';
				echo '<A style="cursor:pointer;">'.$address['ADDRESS'].'<BR>'.($address['CITY']?$address['CITY'].', ':'').$address['STATE'].($address['ZIPCODE']?' '.$address['ZIPCODE']:'').'</A>';
				echo '</TD>';
				echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
				echo '</TR>';
			}
			echo '<TR><TD colspan=2 height=40></TD></TR>';
		}
	}
	elseif(count($addresses_RET)==1)
	{
		if($_REQUEST['address_id']!='new' && $_REQUEST['address_id']!='0')
			$this_address = $addresses_RET[key($addresses_RET)][key($addresses_RET[key($addresses_RET)])][1];

		echo '<TR><TD colspan=3>';
		if($this_address['RESIDENCE']=='Y')
			echo '<IMG SRC=assets/house_button.gif> ';
		if($this_address['MAILING']=='Y')
			echo '<IMG SRC=assets/mailbox_button.gif> ';
		if($this_address['BUS_PICKUP']=='Y' || $this_address['BUS_DROPOFF']=='Y')
			echo '<IMG SRC=assets/bus_button.gif> ';
		echo '</TD></TR>';
		if(AllowEdit())
		{
			echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&modfunc=delete&address_id='.$_REQUEST['address_id']."&LO_index=".$_REQUEST['LO_index'].'\';" onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('remove','','',20).'</TD><TD'.$style.'>';
			echo '<A style="cursor:pointer;"><small>'._('Delete this Address').' &nbsp; </small></A>';
			echo '</TD>';
			echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
			echo '</TR>';
		}
	}
	else
	{
		echo '<TR><TD colspan=3><small>'._('This student doesn\'t have an address.').'</small></TD></TR>';
		if(AllowEdit() && !$_REQUEST['address_id'])
			$_REQUEST['address_id'] = 'new';
	}
	// New Address
	if(AllowEdit())
	{
		$style = '';
		if($_REQUEST['address_id']==='new')
			echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id=new'."&LO_index=".$_REQUEST['LO_index'].'\';" bgcolor=#'.Preferences('HIGHLIGHT').'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD style="color:white;" '.$style.'>';
		else
			echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id=new'."&LO_index=".$_REQUEST['LO_index'].'\';" onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD'.$style.'>';
		echo '<A style="cursor:pointer;"><small>'._('Add an Address').' &nbsp; </small></A>';
		echo '</TD>';
		echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
		echo '</TR>';
		if($_REQUEST['address_id']=='existing')
			echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id=existing'."&LO_index=".$_REQUEST['LO_index'].'\';" bgcolor=#'.Preferences('HIGHLIGHT').'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD style="color:white;" '.$style.'>';
		else
			echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id=existing'."&LO_index=".$_REQUEST['LO_index'].'\';" onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD'.$style.'>';
		echo '<A style="cursor:pointer;"><small>'._('Add an Existing Address').' &nbsp; </small></A>';
		echo '</TD>';
		echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
		echo '</TR>';
		if($_REQUEST['address_id']==='0' && $_REQUEST['person_id']=='new')
			echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id=0&person_id=new'."&LO_index=".$_REQUEST['LO_index'].'\';" bgcolor=#'.Preferences('HIGHLIGHT').'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD '.$link.' style="color:white;" '.$style.'>';
		else
			echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id=0&person_id=new'."&LO_index=".$_REQUEST['LO_index'].'\';" onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD'.$style.'>';
		echo '<A style="cursor:pointer;"><small>'._('Add a Contact<BR>without an Address').' &nbsp; </small></A>';
		echo '</TD>';
		echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
		echo '</TR>';
	}
	echo '</TABLE>';
	echo '</TD>';
	echo '<TD width=10 style="border:1; border-style: none dotted none none;">&nbsp;</TD><TD width=10></TD>';
	if(isset($_REQUEST['address_id']) && $_REQUEST['address_id']!='existing')
	{
		echo '<TD valign=top>';
		echo "<INPUT type=hidden name=address_id value=$_REQUEST[address_id]>";
		if($_REQUEST['address_id']!='0')
		{
			if($_REQUEST['address_id']=='new')
			{
				$size = true;
				$this_address = array('CITY'=>SystemPreferences('DEFAULT_CITY'),'STATE'=>SystemPreferences('DEFAULT_STATE'),'ZIPCODE'=>SystemPreferences('DEFAULT_ZIPCODE'),'PHONE'=>SystemPreferences('DEFAULT_AREACODE'));
				$div = false;
			}
			else
			{
				$size = false;
				$div = 'force';
			}
			
			echo '<TABLE width=100%><TR><TD>';
			echo '<TABLE width=100%>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Address').': </font></TD><TD>'.TextInput($this_address['ADDRESS'],'values[ADDRESS][ADDRESS]','',$size?'size=20':'').'</TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('City').': </font></TD><TD>'.TextInput($this_address['CITY'],'values[ADDRESS][CITY]','','',$div).'</TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('State').': </font></TD><TD>'.TextInput($this_address['STATE'],'values[ADDRESS][STATE]','','size=3',$div).'</TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Zip').': </font></TD><TD>'.TextInput($this_address['ZIPCODE'],'values[ADDRESS][ZIPCODE]','','',$div).'</TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Phone').': </font></TD><TD>'.TextInput($this_address['PHONE'],'values[ADDRESS][PHONE]','',$size?'size=13':'',$div).'</TD></TR>';
			echo '</TABLE>';
			echo '</TD><TD valign=top align=center>';
			if($_REQUEST['address_id']!='new' && $_REQUEST['address_id']!='0')
			{
				$display_address = urlencode($this_address['ADDRESS'].', '.($this_address['CITY']?' '.$this_address['CITY'].', ':'').$this_address['STATE'].($this_address['ZIPCODE']?' '.$this_address['ZIPCODE']:''));
				$link = 'http://google.com/maps?q='.$display_address;
				echo '<A HREF=# onclick=\'window.open("'.$link.'","","scrollbars=yes,resizable=yes,width=800,height=700");\'><IMG SRC=assets/compass_rose_button.gif border=0 width=30><BR><small>'._('Map It').'</small></A>';
			}
			elseif($_REQUEST['address_id'])
			{
				$new = true;
				$this_address['RESIDENCE'] = 'Y';
				$this_address['MAILING'] = 'Y';
				$this_address['BUS_PICKUP'] = 'Y';
				$this_address['BUS_DROPOFF'] = 'Y';
			}
			
			echo'</TD></TR>';
			echo '</TABLE>';
			echo '<TABLE><TR><TD>'.CheckboxInput($this_address['RESIDENCE'],'values[STUDENTS_JOIN_ADDRESS][RESIDENCE]','','CHECKED',$new,'<IMG SRC=assets/check.gif width=15>','<IMG SRC=assets/x.gif width=15>').'</TD><TD><IMG SRC=assets/house_button.gif> '._('Residence').'</TD></TR>';
			echo '<script language=javascript> function show_mailing(checkbox){if(checkbox.checked==true) document.getElementById(\'mailing_address_div\').style.visibility=\'visible\'; else document.getElementById(\'mailing_address_div\').style.visibility=\'hidden\';}</script>';
			if(SystemPreferences('BUS_ADDRESSES')=='Y')
			{
				echo '<TR><TD>'.CheckboxInput($this_address['BUS_PICKUP'],'values[STUDENTS_JOIN_ADDRESS][BUS_PICKUP]','','CHECKED',$new,'<IMG SRC=assets/check.gif width=15>','<IMG SRC=assets/x.gif width=15>').'</TD><TD><IMG SRC=assets/bus_button.gif> '._('Bus Pickup').'</TD></TR>';
				echo '<TR><TD>'.CheckboxInput($this_address['BUS_DROPOFF'],'values[STUDENTS_JOIN_ADDRESS][BUS_DROPOFF]','','CHECKED',$new,'<IMG SRC=assets/check.gif width=15>','<IMG SRC=assets/x.gif width=15>').'</TD><TD><IMG SRC=assets/bus_button.gif> '._('Bus Dropoff').'</TD></TR>';
			}
			if(SystemPreferences('MAILING_ADDRESSES')=='Y')
				echo '<TR><TD>'.CheckboxInput($this_address['MAILING'],'values[STUDENTS_JOIN_ADDRESS][MAILING]','','CHECKED',$new,'<IMG SRC=assets/check.gif width=15>','<IMG SRC=assets/x.gif width=15>',true,'onclick=show_mailing(this);').'</TD><TD><IMG SRC=assets/mailbox_button.gif> '._('Mailing Address').'</TD></TR>';
			echo '</TABLE>';
			if(SystemPreferences('MAILING_ADDRESSES')=='Y')
			{
				echo '<DIV id="mailing_address_div" style="visibility: '.(($this_address['MAILING']||$_REQUEST['address_id']=='new')?'visible':'hidden').';">';
				echo '<TABLE width=100%>';
				echo '<TR><TD colspan=2 style="border-style: solid none none none; border-width: 1;"></TD></TR>';	
				echo '</TABLE>';
		
				echo '<TABLE>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Mailing Address:<BR><small>(If different than above)').'</small> </font></TD><TD>'.TextInput($this_address['MAIL_ADDRESS'],'values[ADDRESS][MAIL_ADDRESS]','',!$this_address['MAIL_ADDRESS']?'size=20':'').'</TD></TR>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('City').': </font></TD><TD>'.TextInput($this_address['MAIL_CITY'],'values[ADDRESS][MAIL_CITY]','').'</TD></TR>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('State').': </font></TD><TD>'.TextInput($this_address['MAIL_STATE'],'values[ADDRESS][MAIL_STATE]','','size=3').'</TD></TR>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Zip').': </font></TD><TD>'.TextInput($this_address['MAIL_ZIPCODE'],'values[ADDRESS][MAIL_ZIPCODE]','').'</TD></TR>';
				echo '</TABLE>';
				echo '</DIV>';
			}
		}
		if($_REQUEST['address_id']!=='new' && $_REQUEST['address_id']!='existing')
		{
			if($_REQUEST['address_id']==0)
				echo '<BR><B><font color=gray>'._('Contacts without an Address').':</font></B>';
			else
				echo '<BR><B><font color=gray>'._('Contacts at this Address').':</font></B>';
			$contacts_RET = DBGet(DBQuery("SELECT p.PERSON_ID,p.FIRST_NAME,p.MIDDLE_NAME,p.LAST_NAME,p.EMAIL,sjp.CUSTODY,sjp.EMERGENCY,sjp.STUDENT_RELATION FROM PEOPLE p,STUDENTS_JOIN_PEOPLE sjp WHERE p.PERSON_ID=sjp.PERSON_ID AND sjp.STUDENT_ID='".UserStudentID()."' AND sjp.ADDRESS_ID='".$_REQUEST['address_id']."'"));
			echo '<TABLE border=0 cellpadding=0 cellspacing=0 width=100%>';
			$i = 1;
			if(count($contacts_RET))
			{
				foreach($contacts_RET as $contact)
				{
					$THIS_RET = $contact;
					if($contact['PERSON_ID']==$_REQUEST['person_id'])
						$this_contact = $contact;
					if($i!=count($contacts_RET))
						$style = ' style="border-color: #BBBBBB; border: 1; border-style: none none dashed none;"';
					else
						$style = ' style="border-color: #BBBBBB; border: 1; border-style: dashed none none none;"';
					$style .= ' ';
					
					$i++;
					$link = 'onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id='.$_REQUEST['address_id'].'&person_id='.$contact['PERSON_ID']."&LO_index=".$_REQUEST['LO_index'].'\';"';
					if(AllowEdit())
						$remove_button = button('remove','',"Modules.php?modname=$_REQUEST[modname]&include=$_REQUEST[include]&modfunc=delete&address_id=$_REQUEST[address_id]&person_id=$contact[PERSON_ID]&LO_index=".$_REQUEST['LO_index'],20);
					else
						$remove_button = '';
					if($_REQUEST['person_id']==$contact['PERSON_ID'])
						echo '<TR bgcolor=#'.Preferences('HIGHLIGHT').'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.$remove_button.'</TD><TD onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id='.$_REQUEST['address_id'].'&person_id='.$contact['PERSON_ID']."&LO_index=".$_REQUEST['LO_index'].'\';" style="color:white;" '.$style.'>';
					else
						echo '<TR onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.$remove_button.'</TD><TD '.$link.' '.$style.'>';
		
					$images = '';
					if($contact['CUSTODY']=='Y')
						$images = ' <IMG SRC=assets/gavel_button.gif> ';
					if($contact['EMERGENCY']=='Y')
						$images .= '<IMG SRC=assets/warning_button.gif> ';
					echo '<A style="cursor:pointer;">'.$contact['FIRST_NAME'].' '.($contact['MIDDLE_NAME']?$contact['MIDDLE_NAME'].' ':'').$contact['LAST_NAME'].($contact['STUDENT_RELATION']?': '.$contact['STUDENT_RELATION']:'').$images.'</A>';
					echo '</TD>';
					echo '<TD'.$style.' '.$link.' valign=middle align='.ALIGN_RIGHT.'> &nbsp; <A style="cursor:pointer;"><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
					echo '</TR>';
				}
			}
			else
			{
				if($_REQUEST['address_id']==0)
					echo '<TR><TD colspan=3><small>'._('This student doesn\'t have any contacts without an address.').'</small></TD></TR>';
				else
					echo '<TR><TD colspan=3><small>'._('This student doesn\'t have any contacts at this address.').'</small></TD></TR>';
			}
			// New Contact
			if(AllowEdit())
			{
				$style = ' style="border:1; border-style: solid none none none;"';
				if($_REQUEST['person_id']=='new')
					echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id='.$_REQUEST['address_id'].'&person_id=new'."&LO_index=".$_REQUEST['LO_index'].'\';" bgcolor=#'.Preferences('HIGHLIGHT').'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD style="color:white;" '.$style.'>';
				else
					echo '<TR onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id='.$_REQUEST['address_id'].'&person_id=new'."&LO_index=".$_REQUEST['LO_index'].'\';" onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD'.$style.'>';
	
				echo '<A style="cursor:pointer;"><small>'._('Add a Contact').'</small></A>';
				echo '</TD>';
				echo '<TD'.$style.' valign=middle align='.ALIGN_RIGHT.'> &nbsp; <A style="cursor:pointer;" align='.ALIGN_RIGHT.'><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
				echo '</TR>';
			}
			echo '</TABLE>';

			$i = 1;
			if($_REQUEST['address_id']!='0')
			{
				echo '<BR><B><font color=gray>'._('Siblings at this Address').':</font></B>';
				echo '<TABLE border=0 cellpadding=0 cellspacing=0 width=100%>';
				$contacts_RET = DBGet(DBQuery("SELECT sam.STUDENT_ID,s.FIRST_NAME,s.LAST_NAME FROM STUDENTS s,STUDENTS_JOIN_ADDRESS sam WHERE sam.STUDENT_ID=s.STUDENT_ID AND sam.ADDRESS_ID='".$_REQUEST['address_id']."' AND sam.STUDENT_ID!='".UserStudentID()."'"));
				if(count($contacts_RET))
				{
					foreach($contacts_RET as $contact)
					{
						if($i!=count($contacts_RET))
							$style = ' style="border-color: #BBBBBB; border: 1; border-style: none none dashed none;"';
						else
							$style = ' style="border-color: #BBBBBB; border: 1; border-style: dashed none none none;"';
						$style .= ' ';
						
						$i++;
						$link = 'onclick="document.location.href=\'Modules.php?modname='.$_REQUEST['modname'].'&include='.$_REQUEST['include'].'&address_id='.$_REQUEST['address_id'].'&person_id='.$contact['PERSON_ID']."&LO_index=".$_REQUEST['LO_index'].'\';"';
						echo '<TR onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="backgroud-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.$remove_button.'</TD><TD '.$link.' '.$style.'>';
						echo '<A style="cursor:pointer;">'.$contact['FIRST_NAME'].' '.($contact['MIDDLE_NAME']?$contact['MIDDLE_NAME'].' ':'').$contact['LAST_NAME'].'</A>';
						echo '</TD>';
						echo '<TD'.$style.' '.$link.' valign=middle align='.ALIGN_RIGHT.'> &nbsp; <A style="cursor:pointer;"><IMG SRC=assets/'.$arrow_right.'.gif></A></TD>';
						echo '</TR>';
					}
				}
				else
					echo '<TR><TD colspan=3><small>'._('This student doesn\'t have any contacts at this address.').'</small></TD></TR>';
				echo '</TABLE>';
			}
		}
		echo '</TD>';
		if(true || $_REQUEST['person_id'])
			echo '<TD width=10 style="border:1; border-style: none dotted none none;">&nbsp;</TD><TD width=10></TD>';
		echo '<TD valign=top>';
		if($_REQUEST['person_id'] && $_REQUEST['address_id']!='new')
		{
			echo "<INPUT type=hidden name=person_id value=$_REQUEST[person_id]>";
			echo '<TABLE>';
			if($_REQUEST['person_id'] && $_REQUEST['person_id']!='new')
			{
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Name').':</font></TD><TD><DIV id=person_'.$this_contact['PERSON_ID'].'><div onclick=\'addHTML("<TABLE cellpadding=0 cellspacing=0><TR><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['FIRST_NAME'],'FIRST_NAME')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['MIDDLE_NAME'],'MIDDLE_NAME')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['LAST_NAME'],'LAST_NAME')).'</TD></TR></TABLE>","person_'.$this_contact['PERSON_ID'].'",true);\'>'.$this_contact['FIRST_NAME'].' '.$this_contact['MIDDLE_NAME'].' '.$this_contact['LAST_NAME'].'</div></DIV></TD></TR>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Relation').': </font></TD><TD colspan=2>'.TextInput($this_contact['STUDENT_RELATION'],'values[STUDENTS_JOIN_PEOPLE]['.$_REQUEST['person_id'].'][STUDENT_RELATION]','','','force').'</TD>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Email').': </font></TD><TD colspan=2>'.TextInput($this_contact['EMAIL'],'values[PEOPLE]['.$_REQUEST['person_id'].'][EMAIL]','','','force').'</TD>';
				echo '<TR><TD colspan=2 align='.ALIGN_RIGHT.'><TABLE><TR><TD>'.CheckboxInput($this_contact['CUSTODY'],'values[STUDENTS_JOIN_PEOPLE]['.$_REQUEST['person_id'].'][CUSTODY]','','','','<IMG SRC=assets/check.gif width=15>','<IMG SRC=assets/x.gif width=15>').'</TD><TD><IMG SRC=assets/gavel_button.gif> '._('Custody').'</TD><TD>'.CheckboxInput($this_contact['EMERGENCY'],'values[STUDENTS_JOIN_PEOPLE]['.$_REQUEST['person_id'].'][EMERGENCY]','','',false,'<IMG SRC=assets/check.gif width=15>','<IMG SRC=assets/x.gif width=15>').'</TD><TD><IMG SRC=assets/warning_button.gif> '._('Emergency').'</TD></TR></TABLE></TD></TR>';
			}
			else
			{
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Name').':</font></TD><TD><TABLE cellpadding=0 cellspacing=0><TR><TD>'.str_replace('"','\"',_makePeopleInput('','FIRST_NAME')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['MIDDLE_NAME'],'MIDDLE_NAME')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['LAST_NAME'],'LAST_NAME')).'</TD></TR></TABLE></TD></TR>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Relation').': </font></TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['STUDENT_RELATION'],'STUDENT_RELATION')).'</TD></TR>';
				echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Email').': </font></TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['EMAIL'],'EMAIL')).'</TD></TR>';
				echo '<TR><TD></TD><TD>'.'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE]['.$_REQUEST['person_id'].'][CUSTODY] value=Y>'.'<IMG SRC=assets/gavel_button.gif> '._('Custody').'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE]['.$_REQUEST['person_id'].'][EMERGENCY] value=Y>'.'<IMG SRC=assets/warning_button.gif> '._('Emergency').'</TD></TR>';
			}
			if($_REQUEST['person_id']!='new')
			{
				echo '<TR><TD colspan=2>';
				echo '<BR><font color=gray><b>'._('Contact Information').':</b></font>';		

				echo '<TABLE border=0 cellpadding=3 cellspacing=0>';
				//echo '<TR><TD style="border-color: #BBBBBB; border: 1; border-style: none none solid none;"></TD><TD style="border-color: #BBBBBB; border: 1; border-style: none solid solid none;"><font color=gray>'._('Description').'</font> &nbsp; </TD><TD style="border-color: #BBBBBB; border: 1; border-style: none none solid none;"><font color=gray>'._('Value').'</font></TD></TR>';
				$info_RET = DBGet(DBQuery("SELECT ID,TITLE,VALUE FROM PEOPLE_JOIN_CONTACTS WHERE PERSON_ID='$_REQUEST[person_id]'"));
				if(count($info_RET))
				{
					foreach($info_RET as $info)
					{
						echo '<TR>';
						if(AllowEdit())
							echo '<TD width=20>'.button('remove','',"Modules.php?modname=$_REQUEST[modname]&include=$_REQUEST[include]&modfunc=delete&address_id=$_REQUEST[address_id]&person_id=$_REQUEST[person_id]&contact_id=".$info['ID']."&LO_index=".$_REQUEST['LO_index']).'</TD>';
						else
							echo '<TD></TD>';
						echo '<TD style="border-color: #BBBBBB; border: 1; border-style: none solid none none;">'.TextInput($info['TITLE'],'values[PEOPLE_JOIN_CONTACTS]['.$info['ID'].'][TITLE]','').'</TD>';
						echo '<TD>'.TextInput($info['VALUE'],'values[PEOPLE_JOIN_CONTACTS]['.$info['ID'].'][VALUE]','').'</TD>';
						echo '</TR>';
					}
					if(AllowEdit())
					{
						echo '<TR>';
						echo '<TD width=20>'.button('add').'</TD>';
						echo '<TD style="border-color: #BBBBBB; border: 1; border-style: none solid none none;"><INPUT size=15 type=TEXT value="'._('Description (ex: Phone)').'" style="color: #BBBBBB;" name=values[PEOPLE_JOIN_CONTACTS][new][TITLE] '."onfocus='if(this.value==\""._('Description (ex: Phone)')."\") this.value=\"\"; this.style.color=\"000000\";' onblur='if(this.value==\"\") {this.value=\""._('Description (ex: Phone)')."\"; this.style.color=\"BBBBBB\";}'></TD>";
						echo '<TD><INPUT size=15 type=TEXT value="(xxx) xxx-xxxx" style="color: #BBBBBB;" name=values[PEOPLE_JOIN_CONTACTS][new][VALUE] '."onfocus='if(this.value==\"(xxx) xxx-xxxx\") this.value=\"\"; this.style.color=\"000000\";' onblur='if(this.value==\"\") {this.value=\"(xxx) xxx-xxxx\"; this.style.color=\"BBBBBB\";}'></TD>";
						echo '</TR>';
					}
				}
				elseif(AllowEdit())
				{
					echo '<TR>';
					echo '<TD width=20>'.button('add').'</TD>';
					echo '<TD style="border-color: #BBBBBB; border: 1; border-style: none solid none none;"><INPUT size=18 type=TEXT value="'._('Description (ex: Phone)').'" style="color: #BBBBBB;" name=values[PEOPLE_JOIN_CONTACTS][new][TITLE] '."onfocus='if(this.value==\""._('Description (ex: Phone)')."\") this.value=\"\"; this.style.color=\"000000\";' onblur='if(this.value==\"\") {this.value=\""._('Description (ex: Phone)')."\"; this.style.color=\"BBBBBB\";}'></TD>";
					echo '<TD><INPUT size=13 type=TEXT value="(xxx) xxx-xxxx" style="color: #BBBBBB;" name=values[PEOPLE_JOIN_CONTACTS][new][VALUE] '."onfocus='if(this.value==\"(xxx) xxx-xxxx\") this.value=\"\"; this.style.color=\"000000\";' onblur='if(this.value==\"\") {this.value=\"(xxx) xxx-xxxx\"; this.style.color=\"BBBBBB\";}'></TD>";
					echo '</TR>';	
				}	
				echo '</TABLE>';	
				echo '</TD></TR>';
			}
			echo '</TABLE>';
		}
		elseif($_REQUEST['address_id']=='new')
		{
			echo '<TABLE>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Relation').': </font></TD><TD>'._makePeopleInput('Mother','STUDENT_RELATION','new').'</TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Name').':</font></TD><TD><TABLE cellpadding=0 cellspacing=0><TR><TD>'.str_replace('"','\"',_makePeopleInput('','FIRST_NAME','new')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['MIDDLE_NAME'],'MIDDLE_NAME','new')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['LAST_NAME'],'LAST_NAME','new')).'</TD></TR></TABLE></TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Email').': </font></TD><TD>'._makePeopleInput('','EMAIL','new').'</TD></TR>';
			echo '<TR><TD></TD><TD>'.'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE][new][CUSTODY] value=Y CHECKED>'.'<IMG SRC=assets/gavel_button.gif> '._('Custody').'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE][new][EMERGENCY] value=Y>'.'<IMG SRC=assets/warning_button.gif> '._('Emergency').'</TD></TR>';
			echo '</TABLE>';
			echo '<HR>';
			echo '<TABLE>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Relation').': </font></TD><TD>'._makePeopleInput('Father','STUDENT_RELATION','new2').'</TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Name').':</font></TD><TD><TABLE cellpadding=0 cellspacing=0><TR><TD>'.str_replace('"','\"',_makePeopleInput('','FIRST_NAME','new2')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['MIDDLE_NAME'],'MIDDLE_NAME','new2')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['LAST_NAME'],'LAST_NAME','new2')).'</TD></TR></TABLE></TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Email').': </font></TD><TD>'._makePeopleInput('','EMAIL','new2').'</TD></TR>';
			echo '<TR><TD></TD><TD>'.'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE][new2][CUSTODY] value=Y CHECKED>'.'<IMG SRC=assets/gavel_button.gif> '._('Custody').'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE][new2][EMERGENCY] value=Y>'.'<IMG SRC=assets/warning_button.gif> '._('Emergency').'</TD></TR>';
			echo '</TABLE>';
			echo '<HR>';
			echo '<TABLE>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Relation').': </font></TD><TD>'._makePeopleInput('Emergency','STUDENT_RELATION','newe').'</TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Name').':</font></TD><TD><TABLE cellpadding=0 cellspacing=0><TR><TD>'.str_replace('"','\"',_makePeopleInput('','FIRST_NAME','newe')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['MIDDLE_NAME'],'MIDDLE_NAME','newe')).'</TD><TD>'.str_replace('"','\"',_makePeopleInput($this_contact['LAST_NAME'],'LAST_NAME','newe')).'</TD></TR></TABLE></TD></TR>';
			echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Email').': </font></TD><TD>'._makePeopleInput('','EMAIL','newe').'</TD></TR>';
			echo '<TR><TD></TD><TD>'.'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE][newe][CUSTODY] value=Y>'.'<IMG SRC=assets/gavel_button.gif> '._('Custody').'<INPUT type=checkbox name=values[STUDENTS_JOIN_PEOPLE][newe][EMERGENCY] value=Y CHECKED>'.'<IMG SRC=assets/warning_button.gif> '._('Emergency').'</TD></TR>';
			echo '</TABLE>';

		}
		echo '</TD>';
	}
	elseif($_REQUEST['address_id']=='existing')
	{
		echo '<TD colspan=2>';
		$siblings_RET = DBGet(DBQuery("SELECT a.ADDRESS_ID,s.STUDENT_ID,s.FIRST_NAME||' '||COALESCE(s.MIDDLE_NAME,' ')||s.LAST_NAME AS FULL_NAME,a.ADDRESS||' '||a.CITY AS ADDRESS,s.STUDENT_ID AS PARENTS FROM STUDENTS s LEFT OUTER JOIN STUDENTS_JOIN_ADDRESS sam ON (sam.STUDENT_ID=s.STUDENT_ID) LEFT OUTER JOIN ADDRESS a ON (a.ADDRESS_ID=sam.ADDRESS_ID) WHERE s.STUDENT_ID!='".$student['STUDENT_ID']."' AND s.LAST_NAME='".$student['LAST_NAME']."'"),array('PARENTS'=>'makeParents'),array('STUDENT_ID'));
		echo '<small>'.$student['FIRST_NAME'].' '.$student['LAST_NAME']._(' shares the same last name with ').count($siblings_RET)._(' other ').MakePlural(count($siblings_RET),_('student'),_('students')).'.</small><BR><BR>';
		if(count($siblings_RET))
		{
			echo '<small>'._('If').' '.$student['FIRST_NAME'].' '.$student['LAST_NAME']._(' is related to any of the students below, click on the sibling\'s name to copy the sibling\'s addresses and contacts.').'</small>';
			$columns = array('FULL_NAME'=>_('Possible Sibling'),'ADDRESS'=>_('Address'),'PARENTS'=>_('Contacts'));
			$link = array('FULL_NAME'=>array('link'=>PreparePHP_SELF().'&modfunc=delete','variables'=>array('sibling_student_id'=>'STUDENT_ID')));
			ListOutput($siblings_RET,$columns,'','',$link,array(array('STUDENT_ID','FULL_NAME')));
		}
		echo '</TD>';
	}
	else
		echo '<TD></TD><TD></TD>';
	echo '</TR>';
	echo '</TABLE>';
	$_REQUEST['category_id'] = 3;
	include('modules/Students/includes/Other_Info.inc.php');
}

function _makePeopleInput($value,$column,$per_id=false)
{	global $THIS_RET;

	if($column=='MIDDLE_NAME')
		$options = 'size=2';
	$div = false;
	if($_REQUEST['person_id']!=='new' && ($column=='STUDENT_RELATION' || $column=='EMAIL'))
		$div = 'force';

	if($per_id===false)
		$per_id = $_REQUEST['person_id'];
	
	if($column=='STUDENT_RELATION')
		$table = 'STUDENTS_JOIN_PEOPLE';
	else
		$table = 'PEOPLE';

	return TextInput($value,'values['.$table.']['.$per_id.']['.$column.']','',$options,$div);
}
?>